package com.pricl.biz.common.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.pricl.biz.common.dao.CommonDao;
import com.pricl.biz.common.entity.ProvinceVO;
import com.pricl.biz.tag.entity.TagInfo;
import com.pricl.frame.cache.CacheListConstant;

/**
 * 公共工具service,常用，非经常修改的结果采用缓存机制
 * @author winton
 *
 */
@Service
public class CommonService {
	
	@Autowired
	private CommonDao commonDao;
	
	/**
	 * 获取省份和城市
	 * @param hasCity 如果true，就查询省份对应的城市信息
	 * @return
	 */
	@Cacheable(key="'"+CacheListConstant.COMMON_SERVICE_KEY+"'+'provinceCity'+'+'+#hasCity", value = CacheListConstant.LOCALREDIS)
	public List<ProvinceVO> getProviceCity(Boolean hasCity) {
		List<ProvinceVO> provinces = commonDao.getProvicne();
		if (hasCity) {
			provinces.forEach(n -> n.setCitys(commonDao.getCity(Maps.newHashMap(ImmutableMap.of("proId",n.getSid())))));
		}
		return provinces;
	}
	
	/**
	 * 获取标签
	 * @return
	 */
	@Cacheable(key="'"+CacheListConstant.COMMON_SERVICE_KEY+"'+'Tag'", value = CacheListConstant.LOCALREDIS)
	public List<TagInfo> getTag() {
		List<TagInfo> tags = commonDao.getTag();
		return tags;
	}
	
}
